require "vector"
require "std"

function d8()
  local object={}

  -- every object needs a mass, angular mass, position, angular velocity, etc, etc.
  object.position=Vector{0,0,0}
  object.mass=1
  object.velocity=Vector{0,0,0}
  object.angularMass=5
  object.angularVelocity=Vector{0,0,0}
  object.color=0xFFFF00
  object.radius=0.9
  -- every object has a list of vertices that take part in the physics simulation.
  local top, bottom={0,0,1},{0,0,-1}
  object.vertices={
    {1,0,0},{0,1,0},{-1,0,0},{0,-1,0},
    top,bottom
  }
  table.transform(object.vertices, Vector)
  
  --we also rescale because it's too big
  --table.transform(object.vertices, function(v) v[1],v[2],v[3]=unpack(v*0.6) return v end)
  
  
  -- every object has a set of surfaces that hold the polygons and the textures
  local polygons=verticesToPolygons(object.vertices)
  object.surfaces=table.transform(polygons,polygonToSurface)
  
  --every surface also has a texture that contains the number image.
  for i,surface in ipairs(object.surfaces) do
    local tb=top
    for i,vertex in ipairs(surface.polygon) do if bottom==vertex then tb=bottom end end
    surface.texture={
      image=i..".png",
      o=surface.center,
      y=2*surface.center-math.avg(tb, surface.center,surface.center)
    }
    surface.texture.x=vector.cross(surface.normal, (surface.texture.y-surface.texture.o))+surface.texture.o
  end
  
  -- every object also has a set of vectors that need to be updated with rotation.
  object.allVertices=extractAllVertices(object)

  -- also surfaces refer back to their parent object
  for i,surface in ipairs(object.surfaces) do 
    surface.object=object
  end
  
return object end
